﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace Throw_In
{
    public partial class Logo : PhoneApplicationPage
    {
        IsolatedStorageFile myFile = IsolatedStorageFile.GetUserStoreForApplication();
        string sFile = "DataTest.txt";

        public Logo()
        {
            InitializeComponent();
            //WebClient wc = new WebClient();
            //wc.DownloadStringCompleted += HttpCompleted;
            //wc.DownloadStringAsync(new Uri("http://www.bolalob.com/api.php/news/detail/api.key.28923403350bc3d2064499/api.secret.196798694850bc3d2064516/20"));
        }
        private void HttpCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("asdsadasd");
            System.Diagnostics.Debug.WriteLine(e.Result);
            if (e.Error == null)
            {
                System.Diagnostics.Debug.WriteLine("asdsd");

                // do something with the XDocument here
            }
        }
        public void LoadData()
        {
            //myFile.DeleteFile(sFile);
            if (!myFile.FileExists(sFile))
            {
                IsolatedStorageFileStream dataFile = myFile.CreateFile(sFile);
                dataFile.Close();
                string sMSG = "hahaha";
                StreamWriter sw = new StreamWriter(new IsolatedStorageFileStream(sFile, FileMode.Append, myFile));
                sw.WriteLine(sMSG); //Wrting to the file
                sw.Close();
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("Ada");
            }

            //Reading and loading data
            StreamReader reader = new StreamReader(new IsolatedStorageFileStream(sFile, FileMode.Open, myFile));
            string rawData = reader.ReadToEnd();
            reader.Close();

            string[] sep = new string[] { "\r\n" }; //Splittng it with new line
            string[] arrData = rawData.Split(sep, StringSplitOptions.RemoveEmptyEntries);

            //List<MyDataClass> dataList = new List<MyDataClass>();
            //foreach (var d in arrData)
            //{
            //    dataList.Add(new MyDataClass() { MSG = d });
            //}
            ////Binding data to the UI for display
            //lstDisplay.ItemsSource = dataList;
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));

        }
        private void BrowserNavigated(object sender, RoutedEventArgs e)
        {
        }
        private void AuthenticateClick(object sender, RoutedEventArgs e)
        {
            var uriParams = new Dictionary<string, string>() {
                        {"client_id", "<your_app_id>"},
                        {"response_type", "token"},
                        {"scope", "user_about_me, offline_access, publish_stream"},
                        {"redirect_uri", "http://www.facebook.com/connect/login_success.html"},
                        {"display", "touch"}
                    };
            StringBuilder urlBuilder = new StringBuilder();
            foreach (var current in uriParams)
            {
                if (urlBuilder.Length > 0)
                {
                    urlBuilder.Append("&amp;");
                }
                var encoded = HttpUtility.UrlEncode(current.Value);
                urlBuilder.AppendFormat("{0}={1}", current.Key, encoded);
            }
            var loginUrl = "http://www.facebook.com/dialog/oauth?" + urlBuilder.ToString();
            //AuthenticationBrowser.Navigate(new Uri(loginUrl));
            //AuthenticationBrowser.Visibility = Visibility.Visible;

        }
    }
}